using BlazorResetModel;

namespace BlazorResetWeb;

public static class CommandRepository
{
	static readonly List<Command> _commands = [];

	public static IList<Command> GetAllCommands() => _commands;


	public static Command? GetCommandById(int id) => _commands.FirstOrDefault(c => c.Id == id);

	public static void UpdateCommand(Command command)
	{
		var cmd = _commands.FirstOrDefault(command => command.Id == command.Id) ?? throw new InvalidOperationException();
		cmd.Name = command.Name;
		cmd.Commands = command.Commands;
	}


	public static void DeleteCommand(int commandId)
	{
		var cmd = _commands.FirstOrDefault(cmd => cmd.Id == commandId) ?? throw new InvalidOperationException();
		_commands.Remove(cmd);
	}

	public static void CreateCommand(Command command)
	{

		var id = _commands is [] ? 1 : _commands.Max(x => x.Id) + 1;
		command.Id = id;
		_commands.Add(command);
	}

}
